home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / strings / searchrt.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-03  |  2.7 KB  |  100 lines

  1. ;unsigned short  search_right(strg,sub_strg,start_pt);
  2. ;  char  *strg,*sub_strg;
  3. ;  unsigned short  start_pt;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _search_right
  11. _search_right proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;
  17.     cmp  _memory_model,0    ;near or far?
  18.     jle  begin        ;jump if near
  19.     inc  bp            ;else add 2 to BP
  20.     inc  bp            ;
  21. begin:    cmp  _memory_model,2    ;data near or far?
  22.     jb   L0            ;jump if near    
  23.     les  di,dword ptr[bp+4] ;ES:DI pts to Strg
  24.     lds  si,dword ptr[bp+8] ;DS:SI pts to Substrg
  25.     add  bp,4        ;adjust stack ptr
  26.     jmp  short L00        ;jump ahead
  27. L0:    mov  ax,ds        ;near case:
  28.     mov  es,ax        ;
  29.     mov  di,[bp+4]        ;
  30.     mov  si,[bp+6]        ;
  31. L00:    mov  bx,[bp+8]        ;startpoint in BX
  32.     inc  bx            ;count from 1
  33.     push di            ;calculate string length
  34.     mov  cx,0ffffh        ;counter
  35.     sub  al,al        ;look for ASCII 0
  36.     cld            ;direction forward
  37.     repne scasb        ;seek end
  38.     not  cx            ;reverse and adjust
  39.     dec  cx            ;length now in CX
  40.     pop di            ;restore Strg ptr
  41.     mov  bp,2        ;2 = Strg null
  42.     jcxz L5            ;quit if null
  43.     push si            ;get length of substring
  44.     sub  ah,ah        ;
  45. L1:    cmp  byte ptr [si],0    ;
  46.     je   L2            ;
  47.     inc  ah            ;
  48.     inc  si            ;
  49.     jmp  short L1        ;
  50. L2:    pop  si            ;    
  51.     inc  bp            ;3 = Substrg null
  52.     or   ah,ah        ;test substring length
  53.     jz   L5            ;quit if null
  54.     dec  bp            ;2 = Startpt out of range
  55.     cmp  bl,cl        ;compare startpt to Strg length
  56.     ja   L5            ;quit if out of range
  57.     dec  bp            ;1 = no match found
  58.     sub  cx,bx        ;string length-startpoint
  59.     inc  cx            ;adjust
  60.     add  di,cx        ;set DI to startpoint
  61.     dec  di            ;adjust
  62.     inc  cx            ;1 extra for SCAS
  63.     mov  al,[si]        ;use char for SCAS below
  64.     inc  si            ;start SCAS from 2nd char
  65. L3:    std            ;direction flag backward
  66.     repne scasb        ;search the 1st char
  67.     jcxz L5            ;jump ahead if not found
  68.     push cx            ;save num chars to scan
  69.     mov  cl,ah        ;substring len in CX
  70.     push si            ;save string pointer
  71.     push di            ;save substring pointer
  72.     inc  di            ;changing direction...
  73.     inc  di            ;...so adjust DI
  74.     cld            ;change direction flag
  75.     repe cmpsb        ;compare the substring
  76.     pop  di            ;restore string pointer
  77.     pop  si            ;restore substring pointer
  78.     jcxz L4            ;substring found if CX=0
  79.     pop  cx            ;set CX for next scan
  80.     jmp  short L3        ;not matching, try again
  81. L4:    pop  cx            ;since pushed before CMPS
  82.     mov  bp,0        ;no error
  83.     jmp  short L6        ;found! quit routine
  84. L5:    mov  cx,1        ;return 0 when not found (DECed)
  85. L6:    mov  ax,cx        ;set result
  86.     dec  ax            ;adjust
  87.     pop  ds            ;restore DS
  88.     mov  bx,bp        ;getch return code
  89.     mov  _error_code,bl    ;set _error_code
  90.     pop  si            ;
  91.     pop  di            ;
  92.     pop  bp            ;
  93.     cmp  _memory_model,0    ;quit
  94.     jle  quit        ;
  95.     db   0CBh        ;RET far
  96. quit:    ret            ;RET near
  97. _search_right ENDP
  98. _TEXT    ENDS
  99.     END
  100.